package starComponent.elements;

import java.awt.Color;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.util.ArrayList;

import starComponent.E_PossibleBranches;

/**
 * @author Charles Perin
 * 11 avr. 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 */
public class StarBranchBinaries extends A_StarBranch{

	private static final long serialVersionUID = -6622621084774458091L;

	private ThumbForMultipleBinaries[] thumbsTab;
	private Shape[]thumbsBoundingBoxes;


	/**
	 * Branche "Slider"
	 * @param star le starComponent associe
	 * @param name le nom de la branche
	 * @param min valeur min
	 * @param max valeur max
	 * @param value valeur par defaut
	 * @param color la couleur de la branche
	 * @param ladder l'echelle de valeurs
	 * @param angle l'angle de la branche : [0;2PI]
	 */
	public StarBranchBinaries(StarComponent star, E_PossibleBranches branch, Color color, Ladder ladder, double angle, boolean isDisplayed) {
		super(star, branch, color, ladder, angle, isDisplayed);
		if(branch.getType()==E_PossibleBranches.MULTIPLE_BINARIES){
			this.thumbsTab = new ThumbForMultipleBinaries[branch.getBinariesTab().length];
			for(int i=0;i<branch.getBinariesTab().length;i++){
				thumbsTab[i] = new ThumbForMultipleBinaries(this, branch.getBinariesTab()[i], i);
			}
			
			this.thumbsBoundingBoxes = new Shape[branch.getBinariesTab().length];
			
		}
		else System.err.println("StarBranchBinaries: erreur a la creation de "+branch.getName());
	}

	@Override
	public String toString(){
		return this.getName();
	}
	
	
	@Override//TODO
	protected void buildBoundingBox(){
	
	}


	/**
	 * parametrise le thumb
	 * @param normalColor les 2 couleurs composant la couleur du thumb au repos
	 * @param selectedColor les 2 couleurs composant la couleur du thumb quand selectionne
	 */
	public void setThumbColors(Color[]normalColor,Color[]selectedColor){
		for(ThumbForMultipleBinaries t:thumbsTab){
			t.setThumbColor(normalColor);
			t.setThumbSelectedColor(selectedColor);
		}
	}
	
	

	public ThumbForMultipleBinaries[] getThumbsTab() {
		return thumbsTab;
	}

	public void setThumbsTab(ThumbForMultipleBinaries[] thumbsTab) {
		this.thumbsTab = thumbsTab;
	}
	
	@Override
	public double getMax(){
		return thumbsTab.length;
	}
	
	public ArrayList<ThumbForMultipleBinaries> getAllThumbsInList(){
		ArrayList<ThumbForMultipleBinaries> tmpList = new ArrayList<ThumbForMultipleBinaries>();
		for(ThumbForMultipleBinaries t:thumbsTab){
			tmpList.add(t);
		}
		return tmpList;
	}

	public ArrayList<Point> getTrueValuesPointsInSortedList(){
		ArrayList<Point>tmpList = new ArrayList<Point>();
		for(ThumbForMultipleBinaries t:thumbsTab){
			if(t.isTrue())tmpList.add(t.getCenter());
		}
		if(tmpList.isEmpty())return null;
		
		ArrayList<Point>list = new ArrayList<Point>();
		Point branchOrigin = new Point((int)this.getxStart(), (int)this.getyStart());
			
		while(list.size()<tmpList.size()){
			Point minPt = null;
			for(Point p:tmpList){
				if(!list.contains(p)){
					minPt=p;
					break;
				}
			}
			for(Point current:tmpList){
				if(!list.contains(current)){
					if(current.distance(branchOrigin)<minPt.distance(branchOrigin)){
						minPt=current;
					}
				}
			}
			list.add(minPt);
		}
		
		return list;
	}
	
	public ArrayList<ThumbForMultipleBinaries> getAllThumbsInSortedList(){
		ArrayList<ThumbForMultipleBinaries>tmpList = new ArrayList<ThumbForMultipleBinaries>();
		for(ThumbForMultipleBinaries t:thumbsTab){
			tmpList.add(t);
		}
		if(tmpList.isEmpty())return null;
		
		ArrayList<ThumbForMultipleBinaries>list = new ArrayList<ThumbForMultipleBinaries>();
		Point branchOrigin = new Point((int)this.getxStart(), (int)this.getyStart());
			
		while(list.size()<tmpList.size()){
			ThumbForMultipleBinaries minThumb = null;
			for(ThumbForMultipleBinaries t:tmpList){
				if(!list.contains(t)){
					minThumb=t;
					break;
				}
			}
			for(ThumbForMultipleBinaries current:tmpList){
				if(!list.contains(current)){
					if(current.getCenter().distance(branchOrigin)<minThumb.getCenter().distance(branchOrigin)){
						minThumb=current;
					}
				}
			}
			list.add(minThumb);
		}
		
		return list;
	}
	


}
